package org.openkoala.koala.wizard.wizardpage;

import java.util.HashSet;
import java.util.Set;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.openkoala.koala.exception.KoalaException;
import org.openkoala.koala.widget.BusinessLog;
import org.openkoala.koala.widget.GeneralQuery;
import org.openkoala.koala.widget.Module;
import org.openkoala.koala.widget.Monitor;
import org.openkoala.koala.widget.Organization;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.widget.Security;
import org.openkoala.koala.wizard.wizardpage.dialog.MonitorConfigDialog;
import org.openkoala.koala.wizard.wizardpage.dialog.SecurityMonitorConfigDialog;

/**
 * 配置诸如权限，监控子系统的集成方式
 * @author xmfang
 *
 */
public class ModuleConfigWizardPage extends WizardPage {

	private Project project;
	
	//要添加统一认证系统的模块
	private Set<Module> selectedModules = new HashSet<Module>();
	
	//控件
	Group modulesGroup;
	
	//监控配置
	Group monitorGroup;
	
	//监控配置
	Group generalQueryGroup;
	
	//组织
	private Group organizationQueryGroup;
	
	private Group bussinessLogGroup;
	 
	private String cacheType = "ehCache";
	
	private String template = "TemplateA";
	
	private String monitorType = "all";
	
	public ModuleConfigWizardPage(Project project) {
		super("SubSystem Config");
		this.project = project;
		setTitle("SubSystem Config");
		setDescription("Configure subsystems and add it to war module.");
	}
	
	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);
		
		chooseViewModule(composite);
		
		setControl(composite);
	}
	
	/**
	 * 显示视图层的模块用于选择
	 * @param composite
	 */
	private void chooseViewModule(Composite composite) {
		GridData modulesGridData = new GridData(GridData.FILL_HORIZONTAL);
		modulesGridData.grabExcessHorizontalSpace = true;

		modulesGroup = new Group(composite, SWT.NULL);
		modulesGroup.setText("Security SubSystem");
		modulesGroup.setLayout(new GridLayout(4, false));
		modulesGroup.setLayoutData(modulesGridData);
		
		monitorGroup = new Group(composite, SWT.NULL);
		monitorGroup.setText("Monitor SubSystem");
		monitorGroup.setLayout(new GridLayout(4, false));
		monitorGroup.setLayoutData(modulesGridData);
		
		generalQueryGroup = new Group(composite, SWT.NULL);
		generalQueryGroup.setText("GeneralQuery SubSystem");
		generalQueryGroup.setLayout(new GridLayout(4, false));
		generalQueryGroup.setLayoutData(modulesGridData);
		
		organizationQueryGroup = new Group(composite, SWT.NULL);
		organizationQueryGroup.setText("Organization SubSystem");
		organizationQueryGroup.setLayout(new GridLayout(4, false));
		organizationQueryGroup.setLayoutData(modulesGridData);
		
		bussinessLogGroup = new Group(composite, SWT.NULL);
		bussinessLogGroup.setText("Bussiness Log SubSystem");
		bussinessLogGroup.setLayout(new GridLayout(4, false));
		bussinessLogGroup.setLayoutData(modulesGridData);
		
		
		createWebModuleCheckBox();
		createConfigModuleCheckBox();
		crateGeneralQueryGroup();
		createOrganizationQueryGroup();
		createBussinessLogGroup();
	}
	
	/**
	 * 刷新CONFIG模块
	 */
	public void refreshWebModules() {
		if (monitorGroup != null) {
			for (Control child : monitorGroup.getChildren()) {
				child.dispose();
			}
			createConfigModuleCheckBox();
			monitorGroup.layout();
		}
		
		if (modulesGroup != null) {
			for (Control child : modulesGroup.getChildren()) {
				child.dispose();
			}
			createWebModuleCheckBox();
			modulesGroup.layout();
		}
		
		if (generalQueryGroup != null) {
			for (Control child : generalQueryGroup.getChildren()) {
				child.dispose();
			}
			crateGeneralQueryGroup();
			generalQueryGroup.layout();
		}
	}
	
	public void refreshMonitorConfig(String type){
		this.monitorType = type;
			for (Module module : project.getModule()) {
				if (module.getModuleType().equals("war")) {
					if(module.getMonitor()!=null){
						module.getMonitor().setInstallType(monitorType);
					}
				}
			}
	}
	
	public void refreshSecurityConfig(String cacheType,String templateType){
		this.cacheType = cacheType;
		this.template = templateType;
		
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				if(module.getSecurity()!=null){
					module.getSecurity().setCacheType(cacheType);
					module.getSecurity().setTemplate(templateType);
				}
			}
		}
	}
	
	private void createConfigModuleCheckBox() {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				//监控也加入。。。。
				final Button monitorAdd = new Button(monitorGroup, SWT.CHECK);
				monitorAdd.setLayoutData(gridData);
				monitorAdd.setText(module.getModuleName());
				monitorAdd.setData(module);
				monitorAdd.addSelectionListener(new SelectionListener(){
					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {
					}
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						if(monitorAdd.getSelection()){
							Module module = (Module)monitorAdd.getData();
							Monitor monitor = new Monitor();
							monitor.setInstallType(monitorType);
							module.setMonitor(monitor);
						}
						if(!monitorAdd.getSelection()){
							Module module = (Module)monitorAdd.getData();
							module.setMonitor(null);
						}
					}
				});
			}
		}
		
		//加入配置
		Button config = new Button(monitorGroup, SWT.PUSH);
		config.setText("Config");
		config.setLayoutData(gridData);
		final ModuleConfigWizardPage moduleConfig = this;
		config.addSelectionListener(new SelectionListener(){
					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {
					}
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						MonitorConfigDialog monitorDialog = new MonitorConfigDialog(Display.getCurrent().getActiveShell(),moduleConfig,monitorType);
						monitorDialog.open();
					}
		});
	}
	
	private void createWebModuleCheckBox() {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				Button ifAdd = new Button(modulesGroup, SWT.CHECK);
				ifAdd.setLayoutData(gridData);
				ifAdd.setText(module.getModuleName());
				ifAdd.setData(module);
				ifAdd.addSelectionListener(new ModuleSelectionListener(this));
			}
		}
		
		//加入配置
				Button config = new Button(modulesGroup, SWT.PUSH);
				config.setText("Config");
				config.setLayoutData(gridData);
				final ModuleConfigWizardPage moduleConfig = this;
				config.addSelectionListener(new SelectionListener(){
							@Override
							public void widgetDefaultSelected(SelectionEvent arg0) {
							}
							@Override
							public void widgetSelected(SelectionEvent arg0) {
								SecurityMonitorConfigDialog securityDialog = new SecurityMonitorConfigDialog(Display.getCurrent().getActiveShell(),moduleConfig,cacheType,template);
								securityDialog.open();
							}
				});
	}
	
	private void crateGeneralQueryGroup() {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				final Button queryWeb = new Button(generalQueryGroup, SWT.CHECK);
				queryWeb.setLayoutData(gridData);
				queryWeb.setText(module.getModuleName());
				queryWeb.setData(module);
				queryWeb.addSelectionListener(new SelectionListener(){

					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {
						
					}

					@Override
					public void widgetSelected(SelectionEvent arg0) {
						if(queryWeb.getSelection()){
							Module module = (Module)queryWeb.getData();
							module.setGeneralQuery(new GeneralQuery());
						}
						if(!queryWeb.getSelection()){
							Module module = (Module)queryWeb.getData();
							module.setGeneralQuery(null);
						}
					}});
			}
		}
	}
	
	private void createBussinessLogGroup(){
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				final Button queryWeb = new Button(this.bussinessLogGroup, SWT.CHECK);
				queryWeb.setLayoutData(gridData);
				queryWeb.setText(module.getModuleName());
				queryWeb.setData(module);
				queryWeb.addSelectionListener(new SelectionListener(){

					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {
						
					}

					@Override
					public void widgetSelected(SelectionEvent arg0) {
						if(queryWeb.getSelection()){
							Module module = (Module)queryWeb.getData();
							module.setBusinessLog(new BusinessLog());
						}
						if(!queryWeb.getSelection()){
							Module module = (Module)queryWeb.getData();
							module.setBusinessLog(null);
						}
					}});
			}
		}
	}
	
	private void createOrganizationQueryGroup() {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				final Button queryWeb = new Button(this.organizationQueryGroup, SWT.CHECK);
				queryWeb.setLayoutData(gridData);
				queryWeb.setText(module.getModuleName());
				queryWeb.setData(module);
				queryWeb.addSelectionListener(new SelectionListener(){

					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {
						
					}

					@Override
					public void widgetSelected(SelectionEvent arg0) {
						if(queryWeb.getSelection()){
							Module module = (Module)queryWeb.getData();
							module.setOrganization(new Organization());
						}
						if(!queryWeb.getSelection()){
							Module module = (Module)queryWeb.getData();
							module.setOrganization(null);
						}
					}});
			}
		}
	}
	
	@Override
	 public IWizardPage getNextPage() {
		IWizardPage result = super.getNextPage();
		if (result.getControl() != null) {
			if (result instanceof InfoSummaryWizardPage) {
				((InfoSummaryWizardPage) result).refreshProjectInfo();
			}
		}
		return result;
	}
	
	
	public String getCacheType() {
		return cacheType;
	}
	
	public void setCacheType(String cacheType) {
		this.cacheType = cacheType;
	}
	
	public String getTemplate() {
		return template;
	}
	
	public void setTemplate(String template) {
		this.template = template;
	}

	public String getMonitorType() {
		return monitorType;
	}

	public void setMonitorType(String monitorType) {
		this.monitorType = monitorType;
	}



	/**
	 * 模块选择复选框的监听器
	 * @author xmfang
	 *
	 */
	private class ModuleSelectionListener implements SelectionListener {
		
		private ModuleConfigWizardPage dialog;
		public ModuleSelectionListener(ModuleConfigWizardPage dialog){
		   this.dialog = dialog;	
		}
		
		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button checkBox = (Button) event.getSource();
			
			Module module = (Module) checkBox.getData();
			if (checkBox.getSelection()) {
				Security security = new Security();
				security.setConnType("JDBC");
				security.setCacheType(dialog.getCacheType());
				security.setTemplate(dialog.getTemplate());
				try {
					module.setSecurity(security);
				} catch (KoalaException e) {
					e.printStackTrace();
				}
				selectedModules.add(module);
			} else {
				try {
					module.setSecurity(null);
				} catch (KoalaException e) {
					e.printStackTrace();
				}
				selectedModules.remove(module);
			}
		}
	}

	/**
	 * 连接协议选择单选框的监听器
	 * @author xmfang
	 *
	 */
	private class ConnectProtocolSelectionListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			if (radio.getSelection()) {
				for (Module module : selectedModules) {
					module.getSecurity().setConnType(String.valueOf(radio.getData()));
				}
			}
		}
	}

	/**
	 * 缓存类型选择单选框的监听器
	 * @author xmfang
	 *
	 */
	private class CacheTypeSelectionListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			if (radio.getSelection()) {
				for (Module module : selectedModules) {
					module.getSecurity().setCacheType(String.valueOf(radio.getData()));
				}
			}
		}
	}

	/**
	 * 模版选择单选框的监听器
	 * @author xmfang
	 *
	 */
	private class TemplateSelectionListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
			
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			if (radio.getSelection()) {
				for (Module module : selectedModules) {
					module.getSecurity().setTemplate(String.valueOf(radio.getData()));
				}
			}
		}
	}
	
}
